import java.util.List;

public class SingleLinkedList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    ListNode head;
    @Override
    public void addFirst(int data) {
        ListNode node =new ListNode(data);//实例化一个单链表
     if(head==null){
         head=node;//如果单链表为空,插入的节点就是头结点
     }else{
         //node节点的next指向head;
         node.next=head;
         head=node;//node变成头节点
     }

    }

    @Override
    public void addLast(int data) {
        ListNode node =new ListNode(data);
        if(head==null){
            head=node;
        }else {
            ListNode cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            //此时cur指向最后一个节点
            cur.next = node;
        }

    }

    @Override
    public void addIndex(int index, int data) {
        if(index<0||index>size()){//判断index是否有效
            throw new IndexException("index越界"+index);
        }
        ListNode node =new ListNode(data);
        if(head == null) {//判断链表是否为空
            head = node;
            return;
        }

        if(index==0){
            //头插
            addFirst(data);
            return;
        }
        if(index==size()){
            //尾插
            addLast(data);
            return;
        }
        //中间插入

        ListNode cur=searchPrevIndex(index);//cur指向index下标的前一个节点
        ListNode curNext=cur.next;
        cur.next=node;
        node.next=curNext;

    }
    private ListNode searchPrevIndex(int index) {
        ListNode cur=head;
        int count=0;
        while(count!=index-1){
            count++;
            cur=cur.next;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head==null){
           return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        ListNode cur=searchPrevRemove(key);
        if(cur==null){
             return;
        }
          ListNode del=cur.next;
          cur.next=del.next;
    }
    private ListNode searchPrevRemove(int key){
        ListNode cur=head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {

        if(head==null){
            return;//单链表为空，直接返回
        }
        ListNode cur=head.next;//从第二个节点判断是否等于key
        ListNode prev=head;
        while(cur!=null){
            if(cur.val==key){
               prev.next=cur.next;
               cur=cur.next;
            }else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){//判断头节点是否==key
            head=head.next;
        }
    }

    @Override
    public int size() {
        ListNode cur=head;
        int count=0;
        while(cur!=null){
            count++;
            cur=cur.next;//cur指向下一个节点
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
     head=null;
    }
}
